package com.dacool.fragment;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.dacool.activity.BaseActivity;
import com.dacool.app.sale.R;
import com.dacool.config.Config;
import com.dacool.http.BaseRequest;
import com.dacool.http.BaseRequest.ResponseHandler;
import com.dacool.http.LogoutRequest;
import com.dacool.util.ViewUtils;

public class SettingsFragment extends BaseFragment implements OnClickListener {

	private Button logoutButton;
	private ViewHolder orderHolder = new ViewHolder();
	private ViewHolder commentsHolder = new ViewHolder();
	private ViewHolder addressHolder = new ViewHolder();
	private ViewHolder coolbHolder = new ViewHolder();
	private ViewHolder recommendHolder = new ViewHolder();
	private ViewHolder aboutsHolder = new ViewHolder();
	private String[] entries = new String[] { "我的订单", "我的库币", "我的推荐", "地址管理",
			"商品评论", "关于大库" };

	@Override
	protected int myLayoutID() {
		// TODO Auto-generated method stub
		return R.layout.frag_settings;
	}

	@Override
	protected View myView() {
		// TODO Auto-generated method stub
		return null;
	}

	// @Override
	// protected boolean showDefaultLeftButton() {
	// // TODO Auto-generated method stub
	// return true;
	// }

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
		// settingsList = ViewUtils.findViewById(view, R.id.settingsList);
		logoutButton = ViewUtils.findViewById(view, R.id.buttonlogout);
		match(orderHolder, view, R.id.setting_orders);
		match(commentsHolder, view, R.id.setting_comments);
		match(addressHolder, view, R.id.setting_addresses);
		match(coolbHolder, view, R.id.setting_coolblog);
		match(aboutsHolder, view, R.id.setting_abouts);
		match(recommendHolder, view, R.id.setting_recommender);
		orderHolder.textLabel.setText(entries[0]);
		coolbHolder.textLabel.setText(entries[1]);
		recommendHolder.textLabel.setText(entries[2]);
		addressHolder.textLabel.setText(entries[3]);
		commentsHolder.textLabel.setText(entries[4]);
		aboutsHolder.textLabel.setText(entries[5]);
		orderHolder.view.setOnClickListener(this);
		addressHolder.view.setOnClickListener(this);
		coolbHolder.view.setOnClickListener(this);
		recommendHolder.view.setOnClickListener(this);
		aboutsHolder.view.setOnClickListener(this);
		aboutsHolder.bottomLine.setVisibility(View.VISIBLE);
		logoutButton.setOnClickListener(this);

	}

	private void match(ViewHolder holder, View view, int id) {
		View v = ViewUtils.findViewById(view, id);
		holder.textLabel = ViewUtils.findViewById(v, R.id.textLabel);
		holder.bottomLine = ViewUtils.findViewById(v, R.id.bottomLine);
		holder.view = v;
		v.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == logoutButton) {
			this.performLogout();

		} else if (v == orderHolder.view) {
			BillListFragment fragment = new BillListFragment();
			getContainerActivity().addFragment(fragment);
		} else if (v == addressHolder.view) {
			AddressListFragment fragment = new AddressListFragment();
			getContainerActivity().addFragment(fragment);
		} else if (v == coolbHolder.view) {
			CoolBFragment fragment = new CoolBFragment();
			getContainerActivity().addFragment(fragment);
		} else if (v == recommendHolder.view) {
			RecommenderListFragment frag = new RecommenderListFragment();
			getContainerActivity().addFragment(frag);

		} else if (v == aboutsHolder.view) {
			WebViewFragment fragment = new WebViewFragment();
			fragment.setUrl("http://app.dacool.com/dcmapp/api/about.html");
			getContainerActivity().addFragment(fragment);
		}
		super.onClick(v);
	}

	private void performLogout() {
		this.showProgressDialog(null, false);
		new LogoutRequest().setHandler(new ResponseHandler() {

			@Override
			public void handleResponse(BaseRequest request, int statusCode,
					String errorMsg, String response) {
				SettingsFragment.this.dismissProgressDialog();
				if (statusCode != 200) {
					ViewUtils.runInMainThread(new Runnable() {

						@Override
						public void run() {
							// TODO Auto-generated method stub
							Toast.makeText(getActivity(), "通讯失败",
									Toast.LENGTH_LONG).show();
						}

					});
					return;
				}
				boolean success = false;
				String errmsg = null;
				try {
					JSONObject json = new JSONObject(response);
					success = json.optBoolean("success", false);
					errmsg = json.optString("msg", null);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (success) {
					ViewUtils.runInMainThread(new Runnable() {

						@Override
						public void run() {
							// TODO Auto-generated method stub
							Toast.makeText(getActivity(), "注销成功",
									Toast.LENGTH_LONG).show();
						}

					});
					Config.sharedConfig().cookies.clear();
					Config.sharedConfig().save();
					ViewUtils.login();
				} else {
					String msg = errmsg != null ? errmsg : "注销失败";
					ViewUtils.showToast(msg, Toast.LENGTH_LONG);

				}
			}
		}).start();

	}

	class ViewHolder {
		TextView textLabel;
		View bottomLine;
		View view;
	}

	@Override
	public String titleText() {
		// TODO Auto-generated method stub
//		Log.d("test", "get title text");
		return "我库";
	}
}
